const app = getApp()
var pwd = "";
Page({
  
  data: {
    password: [],
    password2: [],
    hint: '设置一个解锁密码',
    Length: 4,
    ispassword: false//是否显示星号
  },
  onLoad: function () {
  },

  /**
   * 添加用户解锁密码
   */
  newpassword: function (e) {
    let _this = this
    wx.cloud.callFunction({
      name: 'get_password',
      data: {
        setSelf: true,
        userData: e
      },
      success: res => {
        if (res.errMsg == "cloud.callFunction:ok" && res.result) {
          wx.redirectTo({
            url: '/pages/password/index'
          })
        } else {
          wx.showToast({
            title: '请检查网络您的状态',
            duration: 800,
            icon: 'none'
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '请检查网络您的状态',
          duration: 800,
          icon: 'none'
        })
        console.error("get_password调用失败", err.errMsg)
      }
    })
  },



  keyword: function (e) {
    var _this = this
    //获取本地存的userId
    var userIds = app.globalData.userId

    let that = this;
    let id = e.currentTarget.dataset.id
    let password = that.data.password
    let password2 = that.data.password2
    password.push(id);
    if (password.length == this.data.Length) {
      if (password.join("") == password2.join("")) {
        //调用添加密码的方法
        pwd = ""
        for(let i=0;i<password.length;++i){
          pwd += password[i];
        }
        _this.newpassword({
          userId: userIds,
          user_password: pwd
        })
        wx.showToast({
          title: '设置成功',
          icon: 'none',
          duration: 1000,
        })
        setTimeout(function () {
          wx.redirectTo({
            url: '/pages/password/index'
          })
        }, 1000);
      } else if (password.join("") != password2.join("") && password2.join("") != '') {
        that.unlike()
      } else {
        password2 = password
        that.again()
      }
    }
    that.setData({
      password: password,
      password2: password2
    })

  },
  again: function (e) {
    let that = this
    setTimeout(function () {
      that.data.password = []
      console.log('确认输入密码')
      that.setData({
        password: that.data.password,
        hint: '确认输入密码'
      })
    }, 500);
  },
  unlike: function (e) {
    let that = this
    setTimeout(function () {
      that.data.password = []
      that.data.password2 = []
      console.log('两次输入的密码不正确')
      that.setData({
        password: that.data.password,
        password2: that.data.password2,
        hint: '两次输入的密码不正确'
      })
    }, 500);
  },
  cancel: function (e) {
    let that = this;
    let password = that.data.password
    password = []
    that.setData({
      password: password
    })
  },
  delete: function (e) {
    let that = this;
    let password = that.data.password
    password.pop()
    that.setData({
      password: password
    })
  }
})
